import React from 'react';
import { Provider } from 'react-redux';
import { browserHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import { orange500, cyan500, lightBlue500, indigo500 } from 'material-ui/styles/colors';

import Routes from './routes';
import storeCreater from './redux/store';
import { loginUserSuccess } from './redux/actions/LoginAc';

// 检测是否有token存在,如果存在直接标记为登录成功
// 提取用户名、用户ID
let store = storeCreater(browserHistory);
const history = syncHistoryWithStore(browserHistory, store);
let token = localStorage.getItem('IMLOOKE_COM_USER_TOKEN');
let phone = localStorage.getItem('IMLOOKE_COM_USER_PHONE');
let userId = localStorage.getItem('IMLOOKE_COM_USER_USERID');
if (token !== null) {
  store.dispatch(loginUserSuccess(token, phone, userId));
}

// 主题
const muiTheme = getMuiTheme({
  fontFamily: 'Roboto, Microsoft YaHei, Helvetica, Arial, sans-serif',
  palette: {
    primary1Color: orange500,
    primary2Color: cyan500,
    primary3Color: lightBlue500,
    primary4Color: indigo500,
  },
});

export default class App extends React.Component {
  render() {
    return (
      <MuiThemeProvider muiTheme={muiTheme}>
        <Provider store={store}>
  	  	  <Routes history={history}/>
  	   </Provider>
      </MuiThemeProvider>
    );
  }
};